package com.haiyisoft.eclipse.uep.generate.simple.webpage.wizard;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.eclipse.jdt.core.IType;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerFilter;
import org.eclipse.swt.widgets.Text;

import com.haiyisoft.eclipse.common.util.Utils;

public class TypeFilter extends ViewerFilter {
	
	private Text filterText;
	
	public TypeFilter(Text text){
		this.filterText = text;
	}

	@Override
	public boolean select(Viewer viewer, Object parentElement,
			Object element) {
		String filter = filterText.getText();
		if(Utils.isNullOrEmpty(filter))
			return true;
		filter = filter.toUpperCase();
		Pattern p = Pattern.compile(filter);
		IType type = (IType) element;
		if(filter.indexOf(".") > 0){
			String fullName = type.getFullyQualifiedName().toUpperCase();
			Matcher m2 = p.matcher(fullName);
			if(m2.find())
				return true;
		} else {
			String name = type.getElementName().toUpperCase();
			Matcher m1 = p.matcher(name);
			if(m1.find())
				return true;
		}
		return false;
	}
}
